﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FMSpaceShooter.Screens;
using FMSpaceShooter.ScreenManager;

namespace FMSpaceShooter.GameObjects
{
    sealed class HUD
    {
        private ulong previousHighScore;
        private int margin = 40;
        GameplayScreen screen;
        public HUD(GameplayScreen screen)
        {
            this.screen = screen;
            var scores = Data.HighscoreFile.GetHighscores();
            if (scores.Count > 0)
            {
                previousHighScore = scores.Max(s => s.score);
            }
            else
            {
                previousHighScore = 0;
            }
        }

        internal void Update(GameTime gameTime)
        {

        }

        internal void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;

            Vector2 res = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
                screen.GraphicsDeviceManager.PreferredBackBufferHeight);

            string score = "Score: " + screen.GOM.Score.ToString();
            SpriteFont font = screen.ScreenManager.Font;

            float textWidth = font.MeasureString(score).X;

            Vector2 origin = new Vector2(textWidth, 0);
            Vector2 scorePosition = new Vector2(res.X, 0);
            scorePosition.X -= margin;
            scorePosition.Y += margin;

            spriteBatch.DrawString(font, score, scorePosition, Color.White, 0,
                                   origin, 1, SpriteEffects.None, 0);

            if (previousHighScore < screen.GOM.Score)
            {
                string newScore = "New Highscore!";
                textWidth = font.MeasureString(newScore).X;
                spriteBatch.DrawString(font, newScore, scorePosition + (new Vector2(0, 40)), Color.White, 0,
                                       new Vector2(textWidth, 0), 1, SpriteEffects.None, 0);
            }


            //spriteBatch.DrawString(font, gameTime.ElapsedGameTime.TotalSeconds.ToString(), Vector2.Zero, Color.White, 0,
            //                       Vector2.Zero, 1, SpriteEffects.None, 0);

            Texture2D shipTexture = screen.GOM.GetTexture("Images/Ships/redShip");
            Texture2D negotiatorTexture = screen.GOM.GetTexture("Images/Bullets/javelin");


            for (int i = 0; i < screen.GOM.PlayerOne.Lives; i++)
            {
                Vector2 lifeLocation = new Vector2(0 + margin + (i * 40), res.Y - margin);
                spriteBatch.Draw(shipTexture, lifeLocation, null, Color.White,
                0, Util.Tools.GetTextureVector(shipTexture) / 2, new Vector2(25f, 25f) / Util.Tools.GetTextureVector(shipTexture), SpriteEffects.None, 0);
            }

            Vector2 bombLocation = new Vector2(res.X - margin - 150, res.Y - margin);
            string bombText = "x " + screen.GOM.PlayerOne.Bombs;
            Vector2 bombTextSize = font.MeasureString(bombText);
            Vector2 bombTextLocation = new Vector2(res.X - margin - 125, res.Y - margin);
            spriteBatch.Draw(negotiatorTexture, bombLocation, null, Color.White,
            0, Util.Tools.GetTextureVector(negotiatorTexture) / 2, new Vector2(25f, 25f) / Util.Tools.GetTextureVector(negotiatorTexture), SpriteEffects.None, 0);
            spriteBatch.DrawString(font, bombText, bombTextLocation, Color.White, 0, new Vector2(0, bombTextSize.Y / 2), 1, SpriteEffects.None, 0);

            drawKeys(spriteBatch);
            drawLevel(spriteBatch);
        }

        public void drawLevel(SpriteBatch spriteBatch)
        {
            if ((15 * screen.GOM.Difficulty) - Level.TimeLeft.TotalSeconds < 4)
            {
                string levelText = "Level " + (screen.GOM.Difficulty);

                SpriteFont font = screen.ScreenManager.Font;
                Vector2 res = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
                    screen.GraphicsDeviceManager.PreferredBackBufferHeight);

                Vector2 textWidth = font.MeasureString(levelText);

                spriteBatch.DrawString(font, levelText, res / 2, Color.White, 0,
                                       textWidth / 2, 1, SpriteEffects.None, 0);
            }
        }

        public void drawKeys(SpriteBatch spriteBatch)
        {
            Vector2 res = new Vector2(screen.GraphicsDeviceManager.PreferredBackBufferWidth,
                screen.GraphicsDeviceManager.PreferredBackBufferHeight);
            SpriteFont font = screen.ScreenManager.Font;

            Texture2D keyBack = screen.GOM.GetTexture("Images/keyBack");
            Vector2 keyCenterPosition = new Vector2(res.X / 2, res.Y - 50);
            Vector2 keySize = Util.Tools.GetTextureVector(keyBack);
            Vector2 keySpacing = new Vector2(80, 0);

            List<string> availableList = screen.GOM.PlayerOne.AvailableBullets;
            int activeBullet = screen.GOM.PlayerOne.ActiveBullet;
            Vector2 keyOffset = - keySpacing * (availableList.Count - 1) / 2 ;

            Color keyColor = new Color(255, 255, 255, 100);
            Color keySelectedColor = new Color(255, 255, 0, 100);
            Vector2 letterLocation = new Vector2(15, -25);
            Vector2 levelLocation = new Vector2(15, 10);

            for (int i = 0; i < availableList.Count; i++)
            {
                Vector2 location = keyCenterPosition + keyOffset + (keySpacing * i);

                spriteBatch.Draw(keyBack, location, null, (i == activeBullet ? keySelectedColor : keyColor),
                0, keySize / 2, 1, SpriteEffects.None, 0);
                
                var typeMethod = GameData.Factories.BulletTypes[availableList[i]].GetMethod("get_BulletLevel"); 
                if(typeMethod != null) {
                    int typeLevel = (int)typeMethod.Invoke(null, new object[] { });
                    spriteBatch.DrawString(font, typeLevel.ToString(), location + levelLocation, 
                        Color.White, 0, Vector2.Zero, 0.5f, SpriteEffects.None, 0);
                }

                spriteBatch.DrawString(font, (i + 1).ToString(), location + letterLocation,
                    Color.White, 0, Vector2.Zero, 0.5f, SpriteEffects.None, 0);

                Texture2D bulletTexture = screen.GOM.GetTexture(
                    (string)GameData.Factories.BulletTypes[availableList[i]]
                                    .GetField("imageName").GetValue(null));

                spriteBatch.Draw(bulletTexture, location, null, keyColor,
                0, Util.Tools.GetTextureVector(bulletTexture) / 2, 1, SpriteEffects.None, 0);
            }
        }
    }
}
